package com.turing.user.utils;

import com.alibaba.fastjson.JSONObject;
import com.turing.common.util.Base64Utils;

/**
 * @author ：XiLuo
 * @date ：Created in 2019/6/3 15:22
 * @description：
 * @modified By：
 * @version: $
 */
public class TokenUtils {

    private static final int expiredMills = 7 * 24 * 60 * 60; // 7天

    private static final String salt = "TuringMobile_Salt";

    public static Long getExpiredTime() {
        return System.currentTimeMillis() / 1000 + expiredMills;
    }

    public static String generateToken(String userName, String userId, String companyId, String industryId) {

        JSONObject tokenSalt = new JSONObject();
        tokenSalt.put("salt", salt);

        JSONObject tokenBody = new JSONObject();
        tokenBody.put("userName", userName);
        tokenBody.put("userId", userId);
        tokenBody.put("companyId", companyId);
        tokenBody.put("industryId", industryId);

        JSONObject tokenTail = new JSONObject();
        tokenTail.put("createdTime", System.currentTimeMillis());
        tokenTail.put("expiredAt", System.currentTimeMillis() / 1000 + expiredMills);

        System.out.println("encoding, salt is " + Base64Utils.encode(tokenSalt.toJSONString()));

        String encodedStr = Base64Utils.encode(Base64Utils.encode(tokenSalt.toJSONString()) + Base64Utils.encode(tokenBody.toJSONString() + "_" + tokenTail.toJSONString()));

        return encodedStr;
    }

    public static JSONObject decompileToken(String token) {
        String firstLayerStr = Base64Utils.decode(token);

        JSONObject tokenSalt = new JSONObject();
        tokenSalt.put("salt", salt);
        String salt64 = Base64Utils.encode(tokenSalt.toJSONString());

        int saltIndex = firstLayerStr.indexOf(salt64);
        if (saltIndex != 0) {
            return null;
        }
        String body = firstLayerStr.substring(salt64.length());
        try {
            JSONObject infoObj = new JSONObject();

            String[] decoded = Base64Utils.decode(body).split("_");
            String bodyStr = decoded[0];
            String tailStr = decoded[1];

            JSONObject bodyObj = JSONObject.parseObject(bodyStr);
            JSONObject tailObj = JSONObject.parseObject(tailStr);

            infoObj.putAll(bodyObj);
            infoObj.putAll(tailObj);

            return infoObj;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        String encrypted = generateToken("1234", "1", "1000017", "2");
        System.out.println("encrypted string is = " + encrypted);

        System.out.println(decompileToken(encrypted).toJSONString());

//        System.out.println(decompileToken( "ZXlKellXeDBJam9pVkhWeWFXNW5UVzlpYVd4bFgxTmhiSFFpZlE9PWV5SmpiMjF3WVc1NVNXUWlPaUl4SWl3aWFXNWtkWE4wY25sSlpDSTZJakFpTENKMWMyVnlUbUZ0WlNJNklqRXlNelExTmlJc0luVnpaWEpKWkNJNklqWWlmVjk3SW1WNGNHbHlaV1JCZENJNk1UVTJNRE15TmpVeE55d2lZM0psWVhSbFpGUnBiV1VpT2pFMU5UazNNakUzTVRjNU56Qjk=" ));
    }
}
